<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require 'facebook.php';

function fb_authenticate() {
    $facebook = new Facebook(array('appId' => '118113421612775', 'secret' => '8dd7afe4ff6d5794ccdf6315aaa56de1',));
        return $facebook;
}

function get_user_details($uid, $facebook, $fields, $token, $limit = 10000) {
    return $facebook->api('/' . $uid . '?limit=' . $limit . '&fields=' . $fields . '&access_token=' . $token);
}

function get_friend_ids($uid, $facebook, $user_details) {
    $friend_ids = array();
    foreach ($user_details['friends'] as $detail) {
        foreach ($detail as $index => $value) {
            array_push($friend_ids, $value['id']);
        }
    }
    return $friend_ids;
}

function get_friend_ids_fql($uid,$facebook){
    
}

function get_friend_names($uid, $facebook, $user_details) {
    $friend_names = array();
    foreach ($user_details['friends'] as $detail) {
        foreach ($detail as $index => $value) {
            array_push($friend_names, $value['name']);
        }
    }
    return $friend_names;
}

function get_num_friends($uid, $facebook, $user_details) {
    $friend_ids = array();
    foreach ($user_details['friends'] as $detail) {
        foreach ($detail as $index => $value) {
            array_push($friend_ids, $value['id']);
        }
    }
    return count($friend_ids);
}

function get_like_ids($uid, $facebook, $user_details) {
    $like_ids = array();

    if (array_key_exists('likes', $user_details)) {
        foreach ($user_details['likes'] as $detail) {
            foreach ($detail as $index => $value) {
                array_push($like_ids, $value['id']);
            }
        }
    }

    return $like_ids;
}

function get_group_ids($uid, $facebook, $user_details) {
    $group_ids = array();

    if (array_key_exists('groups', $user_details)) {
        foreach ($user_details['groups'] as $detail) {
            foreach ($detail as $index => $value) {
                array_push($group_ids, $value['id']);
            }
        }
    }
    return $group_ids;
}

function get_photo_tag_ids($uid, $facebook, $user_details) {
    $photo_tag_ids = array();

    if (array_key_exists('photos', $user_details)) {
        foreach ($user_details['photos']['data'] as $detail) {
            array_push($photo_tag_ids, $detail['id']);
        }
    }
   
    return $photo_tag_ids;
}

function get_video_tag_ids($uid, $facebook, $user_details) {
    $video_tag_ids = array();

    if (array_key_exists('videos', $user_details)) {
        foreach ($user_details['videos'] as $detail) {
            foreach ($detail as $index => $value) {
                array_push($video_tag_ids, $value['id']);
            }
        }
    }
    return $video_tag_ids;
}

function get_checkin_ids($uid, $facebook, $user_details) {
    $checkin_ids = array();

    if (array_key_exists('checkins', $user_details)) {
        foreach ($user_details['checkins'] as $detail) {
            foreach ($detail as $index => $value) {
                array_push($checkin_ids, $value['id']);
            }
        }
    }

    return $checkin_ids;
}

function get_event_ids($uid, $facebook, $user_details) {
    $event_ids = array();

    if (array_key_exists('events', $user_details)) {
        foreach ($user_details['events'] as $detail) {
            foreach ($detail as $index => $value) {
                array_push($event_ids, $value['id']);
            }
        }
    }
    return $event_ids;
}

//function get_friends_gender($uid, $facebook, $user_details) {
//    $friends_gender = array();
//    foreach ($user_friend_list['data'] as $friend) {
//        array_push($friends_gender, $friend['gender']);
//    }
//    return $friends_gender;
//}

function get_feed_ids($uid, $facebook, $user_details) {
    $feed_ids = array();

    if (array_key_exists('feed', $user_details)) {
        foreach ($user_details['feed'] as $detail) {
            foreach ($detail as $index => $value) {
                array_push($feed_ids, $value['id']);
            }
        }
    }

    return $feed_ids;
}

function get_feed_interactions($uid, $facebook, $user_details) {

    $ids = array();
    $interactions = array();

    foreach ($user_details['feed']['data'] as $feed_el) {



        if (array_key_exists('comments', $feed_el)) {
            array_push($interactions, $feed_el['comments']);
        }
        if (array_key_exists('likes', $feed_el)) {
            array_push($interactions, $feed_el['likes']);
        }
    }

    foreach ($interactions as $interaction) {
        if (array_key_exists('data', $interaction)) {

            $idata = $interaction['data'];

            foreach ($idata as $element) {
                if (array_key_exists('from', $element)) {
                    array_push($ids, $element['from']['id']);
                } else {
                    array_push($ids, $element['id']);
                }
            }
        }
    }

    return $ids;
}

function count_conversation($uid, $user_feed, $fid, $friend_feed) {
    $user_feed_count = array_count_values($user_feed);
    $conversation_in = array_key_exists($fid, $user_feed_count) ? $user_feed_count[$fid] : 0;

    $friend_feed_count = array_count_values($friend_feed);
    $conversation_out = array_key_exists($uid, $friend_feed_count) ? $friend_feed_count[$uid] : 0;

    return $conversation_in + $conversation_out;
}

function get_employer_ids($uid, $facebook, $user_details) {
    $employer_ids = array();
    if (array_key_exists('work', $user_details)){
            foreach ($user_details['work'] as $work) {
                array_push($employer_ids, $work['employer']['id']);
            }
        
    }
    return $employer_ids;
}

function get_school_ids($uid, $facebook, $user_details) {
    $education_ids = array();
    if (array_key_exists('education', $user_details)){
        foreach($user_details['education'] as $edu){
            array_push($education_ids,$edu['school']['id']);
        }
    }
    return $education_ids;
    
}

function get_num_common($array1, $array2) {

    $intersection = array_intersect($array1, $array2);
    $intersection = array_diff($intersection, array("h"));

    //print_r($intersection);

    return count($intersection);
}

function get_friends_name_and_id($uid, $facebook, $user_details) {

    $ids = get_friend_ids($uid, $facebook, $user_details);
    $names = get_friend_names($uid, $facebook, $user_details);

    $combined = array_combine($ids, $names);

    return $combined;
}

function subval_sort($a, $subkey) {
    $b = array();

    foreach ($a as $k => $v) {
        $b[$k] = $v[$subkey];
    }

    asort($b);

    $c = array(array());

    foreach ($b as $key => $val) {
        $c[$key] = $a[$key];
    }
    return $c;
}


function getMutualFriends($facebook, $uid1, $uid2){
    try {
        $param  =   array('method'  => 'friends.getMutualFriends','source_uid'    => $uid1,'target_uid'  => $uid2,'callback'=> '');
        $mutualFriends   =   $facebook->api($param);
        return count($mutualFriends);
    }
    catch(Exception $o) {
        
    }

    return 0;
}

function normalize($num1, $array){
    $num2 = count($array);

    if ($num2 == 0){
        return 0;
    } else {
        return number_format($num1/$num2, 10);
    }
}

?>
